<?php
$caption = $row['name'];
$restaurants_lbl = BUtils::T('restaurants_lbl');
$this->pageTitle = $caption;
$this->breadcrumbs=array(
	$restaurants_lbl => array('/restaurant'),
	$caption,
);

//define array
$aWeekDaysShort = BUtils::WeekDayKeys(7, true);
$aOpenningHours = BUtils::AllHour24();

//set open hours
$hours_list = '';
if (!empty($aHours)) {
	//set open hours display style
	$sWeekBuffer = array();
	foreach ($aHours as $v) {
		$sWeekday = $aWeekDaysShort[$v['week_no']];
		$sHours = $aOpenningHours[$v['open_hour']] . ' - ' . $aOpenningHours[$v['close_hour']];
		$sWeekBuffer[$sHours][] = $v['week_no'];//add the current week day
	}
	foreach ($sWeekBuffer as $key => $v) {
		$size = count($v);
		if ($size > 1) $sWeekday = $aWeekDaysShort[$v[0]] . '-' . $aWeekDaysShort[$v[$size-1]];
		else $sWeekday = $aWeekDaysShort[$v[0]];
		$hours_list .= '<div class="item"><span class="weekday">'.$sWeekday.'</span><span class="hours">'.$key.'</span></div>';
	}
}

$full_address = $row['address'] . ', ' . $row['postcode'] . ' ' . $row['city'];
$url_logo = BUtils::GetBusinessPictureUrl($row['url_logo']);
$brief = BUtils::GetText($row['brief']);
$opening = BUtils::GetText($row['opening']);
//get the latLng
if (empty($row['latitude']) or empty($row['longitude'])) {
	$latitude = 'null';
	$longitude = 'null';
	$address = "'{$full_address}'";
} else {
	$latitude = $row['latitude'];
	$longitude = $row['longitude'];
	$address = 'null';
}

$tag_labels = array(
	'cuisine_style' => $row['cuisine_style'],
	'cuisine_type' => $row['cuisine_type'],
	'meal_served' => $row['meal_served'],
	'payment_options' => $row['payment_options'],
	'service_languages' => $row['service_languages'],
	'alcohol_license' => $row['alcohol_license'],
	'dining_ambience' => $row['dining_ambience'],
	'car_parking' => $row['car_parking'],
	'additional_details' => $row['additional_details'],
);
?>
<div class="row">
	<div class="span8">
		<div class="box_white relative">
			<div class="panel clearfix place-info">
				<div class="logo">
					<a class="pic" style="display:block; margin-bottom:8px;"><img src="<?php echo $url_logo; ?>" alt="" /></a>
					<?php
					if (Yii::app()->user->isGuest) {
						$btn_name = BUtils::T('follow_btn');
						$link = Yii::app()->user->loginUrl;
						$class = '';
					} else {
						//start to make following button
						if (isset($status['follow']) and $status['follow'] == true) {
							$btn_name = BUtils::T('follow_cancel_btn');
							$link = Yii::app()->createUrl('/user/follow', array('mid'=>$row['id']));
							$class = 'follow-place active';
						} else {
							$btn_name = BUtils::T('follow_btn');
							$link = Yii::app()->createUrl('/user/follow', array('mid'=>$row['id']));
							$class = 'follow-place';
						}
					}
					$this->widget('bootstrap.widgets.TbButton', array(
						'buttonType'=>'link', 'type'=>'info', 'toggle'=>true,
						'label'=>$btn_name, 'url'=>$link,
						'htmlOptions'=>array('class'=>$class, 'style'=>'width:78px;'),
					));
					?>
				</div>
				<h1 class="name"><?php echo $row['name']; ?></h1>
				<div class="address">
					<i class="gicon-google-maps"></i>
					<span class="street"><?php echo $row['address']; ?></span>,
					<span class="city"><?php echo $row['postcode'] . ' ' . $row['city']; ?></span>
				</div>
				<div class="phone">
					<i class="gicon-phone"></i>
					<?php echo $row['phone']; ?>
				</div>
				<?php
				//handle the website link
				if (!empty($row['url_site']) and $row['url_site'] != 'http://') {
					echo '<div class="site-url">
					<i class="gicon-link"></i>
					<a href="'.$row['url_site'].'" target="_blank">'.$row['url_site'].'</a>
				</div>';
				}
				?>
			</div>
<?php
//tags
echo '<div style="padding:8px; text-align:right; margin-top:-16px;">';
$tags_number = 0;
foreach ($tag_labels as $k => $v) {
	if (!empty($v)) {
		$aRow = explode(',', $v);
		$tags_number ++;
		$class = $tags_number > 2 ? 'tag-more' : '';
		$style = $tags_number > 2 ? 'background:#0ae;' : 'background:#0ae;';
		echo '<span class="label tag-more" style="cursor:pointer;">'.BUtils::T($k . '_lbl').':</span>' . "\n";
		foreach ($aRow as $key) {
			$this->widget('bootstrap.widgets.TbLabel', array(
				'type'=>'default', 'label'=>BUtils::T($key),
				'htmlOptions' => array('style'=>$style, 'class'=>$class),
			));
			echo "\n";
		}
		echo '<br class="tag-more" \>';
	}
}
if ($tags_number > 2) {//add a more tag here
	echo "\n" . '<span class="label more-tags" style="cursor:pointer;">more</span>';
}
echo "</div>\n";
?>
		</div>

		<div class="row clearfix">
<?php
if (empty($gallery_rows)) {
	$classSpan = 'span8';
} else {
	$classSpan = 'span5';
	$pictureLinks = '';
	foreach ($gallery_rows as $gal) {
		$link = BUtils::GetBusinessPictureUrl($gal['url_picture']);
		$thumb200Link = str_replace('thumb_', 'thumb_200h_', $link);
		$fileLink = str_replace('thumb_', '', $link);
		$pictureLinks .= '<a class="thumb" href="'.$fileLink.'" title="'.$gal['title'].'"><img src="'.$thumb200Link.'" alt="" /></a> ';
	}
	echo <<<HTML
<div class="span3">
	<div class="box_white fill-right place-info">
		<div class="media fixed-height gallery-space">
			{$pictureLinks}
		</div>
	</div>
</div>
HTML;
}
?>
			<div class="<?php echo $classSpan; ?>">
				<div class="box_white">
					<div id="venueMap" class="media fixed-height" data-longitude="<?php echo $longitude; ?>" data-latitude="<?php echo $latitude; ?>"></div>
				</div>
			</div>
		</div>
		<div class="row clearfix">
<?php if (!empty($special_rows)) { ?>
			<div class="span8">
				<div class="box_white">
					<div class="title-bar">
						<h4 class="red"><?php echo BUtils::T('special_lbl'); ?></h4>
					</div>
					<div class="panel panel-with-title">
<?php
foreach ($special_rows as $k => $spc) {
	//valida date
	$selected_valid_date = BUtils::GetDateRangeString($spc['uts_start_date'], $spc['uts_end_date'], 'd.m');
	// valid time
	$selected_hour_range = BUtils::GetHourRangeFormat($spc['start_hour'], $spc['end_hour']);
	// week days
	$selected_week_days = BUtils::HtmlWeekdays($spc['week_days'], $aWeekDaysShort);
	//discount
	$discount_text = null;
	if (!empty($spc['discount'])) {
		$discount_text = '-' . round($spc['discount']) . '%';
	}
	//title
	$title_text = BUtils::T('special_auto_title_lbl', array(
		'{merchant_name}' => $row['name'],
		'{price}' => Yii::app()->locale->numberFormatter->formatCurrency($spc['price'], 'EUR'),
		'{value}' => Yii::app()->locale->numberFormatter->formatCurrency($spc['value'], 'EUR'),
	));
	$link_url = Yii::app()->createUrl('/special/view', array('id'=>$spc['id']));
	$link_button = CHtml::link($title_text, $link_url, array('class'=>'preview-btn'));
	echo <<<HTML
<div class="specialItem clearfix">
	<div class="discount-box">
		<span>{$discount_text}</span>
	</div>
	<div class="detail-box">
		<div class="title">{$link_button}</div>
		<div class="period clearfix">
			<div class="left">{$selected_hour_range}</div>
			<div class="right">{$selected_valid_date}</div>
		</div>
		<div class="week">{$selected_week_days}</div>
	</div>
</div>
HTML;
}
?>
					</div>
				</div>
			</div>
<?php } ?>
<?php if (!empty($otherDishes)) { ?>
			<div class="span8">
				<div class="box_white">
					<div class="title-bar">
						<h4 class="green"><?php echo BUtils::T('dish_featured_lbl'); ?></h4>
					</div>
					<div class="panel panel-with-title">
<?php
foreach ($otherDishes as $i => $d) {
	$number = $i + 1;
	$urlThumb = BUtils::GetBusinessPictureUrl($d['url_picture']);
	$price = Yii::app()->locale->numberFormatter->formatCurrency($d['price'], 'EUR');
	$name = CHtml::encode($d['name']);
	$link = Yii::app()->createUrl('/dish/view', array('id'=>$d['id']));
	echo <<<HTML
<div class="venueItem clearfix">
	<div class="dishItem">
		<div class="number">{$number}</div>
		<div class="picture"><a href="{$link}"><img src="{$urlThumb}" width="76" alt="{$name}" class="img-polaroid" /></a></div>
		<div class="name"><a href="{$link}">{$name}</a></div>
		<div class="price">{$price}</div>
	</div>
</div>
HTML;
}
?>
					</div>
				</div>
			</div>
<?php } ?>
		</div>
	</div>

	<!-- right side -->
	<div class="span4">
		<div class="box_white bottom-space more-tags-slider" style="display:none;">
			<div class="title-bar">
				<h4 class="blue"><?php echo BUtils::T('detail_features_lbl'); ?></h4>
			</div>
			<div class="panel panel-with-title clearfix">
<?php
foreach ($tag_labels as $key => $value) {
	if (!empty($value)) {
		$bizTagLinks = $this->getTagLinks($value, ', ');
		echo '<div class="tag-title">'.BUtils::T($key . '_lbl').':</div>
			<div class="tag-detail">'.$bizTagLinks.'</div>';
	}
}
?>
			</div>
		</div>
		<div class="box_white">
			<div class="title-bar">
				<h4 class="blue"><?php echo BUtils::T('merchant_brief_lbl'); ?></h4>
			</div>
			<div class="panel panel-with-title">
				<div class="dishItems clearfix">
					<?php echo $brief; ?>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
$(function() {
	//more tags
	$('.more-tags').toggle(function() {
		$(this).html('less');
		$('.tag-more').show();
	}, function() {
		$(this).html('more');
		$('.tag-more').hide();
	})
	
	//popup special box and gallery
	$('.preview-btn').colorbox({rel:'coupon',transition:'none'});
	$('.gallery-space a.thumb').colorbox({rel:'thumb',transition:'none'});
	
	//init the map
	var mapCav = $("#venueMap").isMapCanvas('<?php echo Yii::app()->baseUrl; ?>', 1);
	mapCav.pin('mid_<?php echo $row['name']; ?>', {
		style:3,
		latitude: <?php echo $latitude; ?>,
		longitude: <?php echo $longitude; ?>
	});
});
</script>